package com.googlecode.ozonbooksfinder.parser
{
	import com.googlecode.ozonbooksfinder.domain.Book;
	
	import mx.collections.ArrayCollection;
	
	public class BooksParser
	{
		public static function parseSearchResult (source:Object):ArrayCollection
		{
			var sourceXml:XML = getXmlFromSource(source);
			if (sourceXml)
			{
				var declarations:Array = sourceXml.namespaceDeclarations();
				
				var soap:Namespace = sourceXml.namespace();
				var ozon:Namespace = new Namespace ("http://www.ozon.ru/");
				var diffgr:Namespace = new Namespace ("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");
				
				var searchList:XMLList = sourceXml.ozon::SearchWebServiceResponse.
					ozon::SearchWebServiceResult.
					diffgr::diffgram.DocumentElement.SearchItems;
				
				var result:ArrayCollection = new ArrayCollection ();
				for each (var bookXml:XML in searchList)
				{
					var book:Book = parseBook (bookXml);
					result.addItem(book);
				}
				return result;
			}
			return null;
		}
		
		public static function parseItemDetail (source:Object):Book
		{
			var sourceXml:XML = getXmlFromSource(source);
			if (sourceXml)
			{
				var declarations:Array = sourceXml.namespaceDeclarations();
				
				var soap:Namespace = sourceXml.namespace();
				var ozon:Namespace = new Namespace ("http://www.ozon.ru/webservices/");
				var diffgr:Namespace = new Namespace ("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");
				
				var itemDetail:XML = sourceXml.ozon::ItemDetailResponse.
					ozon::ItemDetailResult.
					diffgr::diffgram.ItemDetailData.ItemDetail [0];
					
				var result:Book = itemDetail ? parseBook (itemDetail) : null;
				return result;
			}
			return null;
		}
		
		public static function parseItemInfo (source:Object):Book
		{
			var sourceXml:XML = getXmlFromSource(source);
			if (sourceXml)
			{
				var declarations:Array = sourceXml.namespaceDeclarations();
				
				var soap:Namespace = sourceXml.namespace();
				var ozon:Namespace = new Namespace ("http://www.ozon.ru/webservices/");
				var diffgr:Namespace = new Namespace ("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");
				
				var itemDetail:XML = sourceXml.ozon::ItemInfoResponse.
					ozon::ItemInfoResult.
					diffgr::diffgram.ItemInfoData.ItemInfo [0];
					
				var result:Book = itemDetail ? parseBook (itemDetail) : null;
				return result;
			}
			return null;
		}
		
		private static function getXmlFromSource (source:Object):XML
		{
			if (source is XML)
			{
				return source as XML;
			}
			
			if (source is String)
			{
				return new XML (source);
			}
			
			return null;
		}
		
		private static function parseBook (bookXml:XML):Book
		{
			var book:Book = new Book ();
			
			book.id = parseInt(bookXml.ID);
			book.name = ((bookXml.Name != null) && 
				(bookXml.Name.toString ().length > 0)) ? 
				bookXml.Name : null;
			book.otherName = ((bookXml.OtherName != null) && 
				(bookXml.OtherName.toString ().length > 0)) ? 
				bookXml.OtherName : null;
			book.author = ((bookXml.Author != null) && 
				(bookXml.Author.toString ().length > 0)) ? 
				bookXml.Author : null;
			book.year = ((bookXml.Year != null) && 
				(bookXml.Year.toString ().length > 0)) ? 
				parseInt(bookXml.Year) : -1;
			book.annotation = ((bookXml.Annotation != null) && 
				(bookXml.Annotation.toString ().length > 0)) ? 
				bookXml.Annotation : null;
			book.imagePath = ((bookXml.Picture != null) && 
				(bookXml.Picture.toString ().length > 0)) ? 
				bookXml.Picture : (((bookXml.ImagePath != null) && 
				(bookXml.ImagePath.toString ().length > 0)) ? 
				bookXml.ImagePath : null);
			book.itemType = ((bookXml.ItemType != null) && 
				(bookXml.ItemType.toString ().length > 0)) ? 
				bookXml.ItemType : null;
			book.price = ((bookXml.Price != null) && 
				(bookXml.Price.toString ().length > 0)) ? 
				parseFloat (bookXml.Price) : NaN;
			book.url = ((bookXml.Url != null) && 
				(bookXml.Url.toString ().length > 0)) ? 
				bookXml.Url : null;
			book.availability = ((bookXml.Availability != null) && 
				(bookXml.Availability.toString ().length > 0)) ? 
				bookXml.Availability : null;
			book.weight = ((bookXml.weight != null) && 
				(bookXml.weight.toString ().length > 0)) ? 
				parseInt(bookXml.weight) : -1;
			book.specialPrice = ((bookXml.SpecialPrice != null) && 
				(bookXml.SpecialPrice.toString ().length > 0)) ? 
				parseFloat (bookXml.SpecialPrice) : NaN;
			
			return book;
		}
	}
}